package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.WyExcelTestVO;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.ExportExcelUtil;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class Wangyi1 {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "_ntes_nnid=2ba9c27042ad7c5ab73d485ec917f2b8,1627024350767; _ntes_nuid=2ba9c27042ad7c5ab73d485ec917f2b8; __oc_uuid=584a1580-eb85-11eb-a7f2-2faf5ac727dc; _antanalysis_s_id=1627024351213";
    public static final String URL = "https://open.163.com/cuvocw/";
    public static final String URL_PRE = "https://open.163.com/cuvocw/";
    public static final String PREFIX = "网易公开课-中国大学视频公开课-";


    @Test
    public void initCrawler() throws Exception {
        String path1 = "F:\\1.txt";
        String path2 = "F:\\2.txt";
        String path3 = "F:\\3.txt";
        String path4 = "F:\\4.txt";
        String path5 = "F:\\5.txt";
        String path6 = "F:\\6.txt";
        List<String> pathList = new ArrayList<>();
        pathList.add(path1);
        pathList.add(path2);
        pathList.add(path3);
        pathList.add(path4);
        pathList.add(path5);
        pathList.add(path6);
        for (String path : pathList) {
            doCrawler(getHtmlStr(path));
        }
    }

    public String getHtmlStr(String path) {
        FileReader file1Reader = new FileReader(path);
        String result = file1Reader.readString();
        return result;

    }

    public void doCrawler(String htmlStr) throws Exception {
//        Map<String, Object> parameter = MapUtil.newHashMap(0);
//        Map<String, Object> headers = MapUtil.newHashMap(2);
//        String result = HttpUtil.doGet(URL, headers, parameter);
//        int tryCount = 1;
//        while (result.contains("页面没有找到") && tryCount <= 5) {
//            //睡眠2秒继续访问
//            Thread.sleep(2000);
//            result = HttpUtil.doPost(URL, headers, parameter);
//            tryCount++;
//        }
        if (StrUtil.isNotBlank(htmlStr)) {
            Document htmlDoc = Jsoup.parse(htmlStr);
            String fileName = "";
            Elements tags = htmlDoc.select(".u-tag4");
            if (tags != null && tags.size() > 0) {
                fileName = tags.get(0).select("h2").text();
            } else {
                fileName = Convert.toStr(System.currentTimeMillis());
            }
            Elements bookContentList = htmlDoc.select(".u-cover");
            List<WyExcelTestVO> dataList = new ArrayList<>();
            if (bookContentList != null && bookContentList.size() > 0) {
                for (Element element : bookContentList) {
                    if (element != null) {
                        //详情地址
                        String href = element.attr("href");
                        if (StrUtil.isNotEmpty(href)) {
                            String pid = href.split("newurl=")[1];
                            if (StrUtil.isNotEmpty(pid)) {
                                WyExcelTestVO entity = getBookEntity(pid);
                                dataList.add(entity);
                            }
                        }
                    }
                }
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("dataList", dataList);
                Workbook workbook = ExportExcelUtil.exportExcel("/mapper/template/excel/excelTest.xlsx", dataMap);
                OutputStream os = new FileOutputStream("F:/" + PREFIX + fileName + ".xls");
                workbook.write(os);
            }
        }

    }

    public WyExcelTestVO getBookEntity(String pid) throws Exception {
        WyExcelTestVO bookEntity = null;
        if (StrUtil.isBlank(pid)) {
            return null;
        }
        String url = "https://c.open.163.com/open/mob/movie/list.do?plid=" + pid;
        Map<String, Object> headers = MapUtil.newHashMap(1);
        String result = HttpUtil.doGet(url, headers);
        int tryCount = 1;
        while (result.contains("页面没有找到") && tryCount <= 5) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doGet(url, headers);
            tryCount++;
        }
        if (StrUtil.isNotBlank(result)) {
//            Document htmlDoc = null;
//            try {
//                htmlDoc = Jsoup.parse(result);
//            } catch (Exception e) {
//                log.warn("jsoup解析html发生错误");
//            }
//            if (htmlDoc == null) {
//                return null;
//            }
//            Elements title = htmlDoc.select(".content-title");
//            if (CollUtil.isNotEmpty(title)) {
//                String titleStr = title.text();
//                bookEntity.setTitle(titleStr);
//            }
//            Elements courseIntro = htmlDoc.select(".course-intro");
//            if (CollUtil.isNotEmpty(courseIntro)) {
//                Element contentLeft = courseIntro.select(".content-left img").get(0);
//                String coverStr = contentLeft.attr("src");
//                bookEntity.setCover(coverStr);
//            }
//            Elements intro = htmlDoc.select(".second-intro").select("P");
//            if (CollUtil.isNotEmpty(intro)) {
//                try {
//                    String lecturerStr = intro.get(0).text();
//                    if (lecturerStr.contains("讲师")) {
//
//                    }
//                    bookEntity.setLecturer(lecturerStr);
//                } catch (Exception e) {
//                }
//                try {
//                    String schoolStr = intro.get(1).text();
//                    bookEntity.setSchool(schoolStr);
//                } catch (Exception e) {
//                }
//                try {
//                    String introStr = intro.get(2).text();
//                    bookEntity.setIntro(introStr);
//                } catch (Exception e) {
//
//                }
//
//            }
//            Element listLink = htmlDoc.select(".list-show").select(".list-link a").first();
//            if (listLink != null) {
//                String urlStr = listLink.attr("href");
//                bookEntity.setUrl("https://open.163.com" + urlStr);
//            }
        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
